﻿/// <summary>
/// https://leetcode-cn.com/problems/roman-to-integer/
/// </summary>
public class Solution
{
    private enum RomanDigit : int
    {
        I = 1,
        IV = 4,
        V = 5,
        IX = 9,
        X = 10,
        XL = 40,
        L = 50,
        XC = 90,
        C = 100,
        CD = 400,
        D = 500,
        CM = 900,
        M = 1000
    }
    public int RomanToInt(string s)
    {
        var romanDigits = Enum.GetValues<RomanDigit>().
            OrderByDescending((digit) => digit.ToString().Length);

        var result = 0;
        foreach (var digit in romanDigits)
        {
            var originalLength = s.Length;
            s = s.Replace(digit.ToString(), "");
            var newLength = s.Length;
            result += (int)digit * ((originalLength - newLength) / digit.ToString().Length);
        }
        return result;
    }
}